/**
 * 作者: wgx
 * 日期: 2025-03-31 10:30:51
 * 备注: 用户中餐厅模块表信息表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserRestaurantInfo;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserRestaurantInfoModel extends BaseModel implements Model {
    public static final String LISTKEY = "";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // 玩家id
    protected String unlockRoom = ""; // 已解锁的餐厅id列表
    protected String unlockFood = ""; // 已解锁的美食攻略id列表
    protected String unlockGuest = ""; // 已解锁的客人id列表
    protected int score; // 人气总值
    protected int totalGuest; // 总的接待人数
    protected int receptionNum; // 剩余可用接待次数
    protected long receptionTime; // 接待次数的上次恢复时间
    protected String taskReward = ""; // 任务奖励领取记录

    public void reset() {
    }

    protected String getKey() {
        return "UserRestaurantInfo." + id;
    }

    protected String getListKey() {
        return null;
    }

    protected String getListItemKey() {
        return null;
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_restaurant_info"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, id, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, id, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // 玩家id
        obj.put("unlockRoom", unlockRoom); // 已解锁的餐厅id列表
        obj.put("unlockFood", unlockFood); // 已解锁的美食攻略id列表
        obj.put("unlockGuest", unlockGuest); // 已解锁的客人id列表
        obj.put("score", score); // 人气总值
        obj.put("totalGuest", totalGuest); // 总的接待人数
        obj.put("receptionNum", receptionNum); // 剩余可用接待次数
        obj.put("receptionTime", receptionTime); // 接待次数的上次恢复时间
        obj.put("taskReward", taskReward); // 任务奖励领取记录
        return obj;
    }

    public PBUserRestaurantInfo.Builder takeInitBuilder() {
        PBUserRestaurantInfo.Builder sb = PBUserRestaurantInfo.newBuilder();
        if (this.unlockRoom != null && this.unlockRoom.length() > 0) {
            JsonArray list = new JsonArray(this.unlockRoom);
            for(int i=0; i<list.size(); i++) {
                sb.addUnlockRoom(list.getInteger(i));
            }
        }
        if (this.unlockFood != null && this.unlockFood.length() > 0) {
            JsonArray list = new JsonArray(this.unlockFood);
            for(int i=0; i<list.size(); i++) {
                sb.addUnlockFood(list.getInteger(i));
            }
        }
        if (this.unlockGuest != null && this.unlockGuest.length() > 0) {
            JsonArray list = new JsonArray(this.unlockGuest);
            for(int i=0; i<list.size(); i++) {
                sb.addUnlockGuest(list.getInteger(i));
            }
        }
        if (score != 0) {
            sb.setScore(score);
        }
        if (totalGuest != 0) {
            sb.setTotalGuest(totalGuest);
        }
        if (receptionNum != 0) {
            sb.setReceptionNum(receptionNum);
        }
        if (receptionTime != 0) {
            sb.setReceptionTime(receptionTime);
        }
        if (this.taskReward != null && this.taskReward.length() > 0) {
            JsonArray list = new JsonArray(this.taskReward);
            for(int i=0; i<list.size(); i++) {
                sb.addTaskReward(list.getInteger(i));
            }
        }
        return sb;
    }

    public PBUserRestaurantInfo.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserRestaurantInfo.Builder sb = PBUserRestaurantInfo.newBuilder();
        if (isFieldUpdate("unlockRoom")) {
            if (this.unlockRoom != null && this.unlockRoom.length() > 0) {
                JsonArray list = new JsonArray(this.unlockRoom);
                for(int i=0; i<list.size(); i++) {
                    sb.addUnlockRoom(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearUnlockRoom();
                }
            } else {
                sb.clearUnlockRoom();
            }
        }
        if (isFieldUpdate("unlockFood")) {
            if (this.unlockFood != null && this.unlockFood.length() > 0) {
                JsonArray list = new JsonArray(this.unlockFood);
                for(int i=0; i<list.size(); i++) {
                    sb.addUnlockFood(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearUnlockFood();
                }
            } else {
                sb.clearUnlockFood();
            }
        }
        if (isFieldUpdate("unlockGuest")) {
            if (this.unlockGuest != null && this.unlockGuest.length() > 0) {
                JsonArray list = new JsonArray(this.unlockGuest);
                for(int i=0; i<list.size(); i++) {
                    sb.addUnlockGuest(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearUnlockGuest();
                }
            } else {
                sb.clearUnlockGuest();
            }
        }
        if (isFieldUpdate("score")) {
            sb.setScore(score);
        }
        if (isFieldUpdate("totalGuest")) {
            sb.setTotalGuest(totalGuest);
        }
        if (isFieldUpdate("receptionNum")) {
            sb.setReceptionNum(receptionNum);
        }
        if (isFieldUpdate("receptionTime")) {
            sb.setReceptionTime(receptionTime);
        }
        if (isFieldUpdate("taskReward")) {
            if (this.taskReward != null && this.taskReward.length() > 0) {
                JsonArray list = new JsonArray(this.taskReward);
                for(int i=0; i<list.size(); i++) {
                    sb.addTaskReward(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearTaskReward();
                }
            } else {
                sb.clearTaskReward();
            }
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public JsonArray getUnlockRoomArray() {
        if (this.unlockRoom != null && this.unlockRoom.length() > 0) {
            return new JsonArray(this.unlockRoom);
        } else {
            return new JsonArray();
        }
    }

    public String getUnlockRoom() {
        return unlockRoom;
    }

    public void putUnlockRoom(String unlockRoom) {
        if (!this.unlockRoom.equals(unlockRoom)) {
            this.unlockRoom = unlockRoom;
            updateRedisHashField(getKey(), "unlockRoom", this.unlockRoom);
        }
    }

    public JsonArray getUnlockFoodArray() {
        if (this.unlockFood != null && this.unlockFood.length() > 0) {
            return new JsonArray(this.unlockFood);
        } else {
            return new JsonArray();
        }
    }

    public String getUnlockFood() {
        return unlockFood;
    }

    public void putUnlockFood(String unlockFood) {
        if (!this.unlockFood.equals(unlockFood)) {
            this.unlockFood = unlockFood;
            updateRedisHashField(getKey(), "unlockFood", this.unlockFood);
        }
    }

    public JsonArray getUnlockGuestArray() {
        if (this.unlockGuest != null && this.unlockGuest.length() > 0) {
            return new JsonArray(this.unlockGuest);
        } else {
            return new JsonArray();
        }
    }

    public String getUnlockGuest() {
        return unlockGuest;
    }

    public void putUnlockGuest(String unlockGuest) {
        if (!this.unlockGuest.equals(unlockGuest)) {
            this.unlockGuest = unlockGuest;
            updateRedisHashField(getKey(), "unlockGuest", this.unlockGuest);
        }
    }

    public int getScore() {
        return score;
    }

    public void putScore(int score) {
        if (this.score != score) {
            this.score = score;
            updateRedisHashField(getKey(), "score", this.score);
        }
    }

    public void addScore(int value) {
        this.score += value;
        updateRedisHashField(getKey(), "score", this.score);
    }

    public int getTotalGuest() {
        return totalGuest;
    }

    public void putTotalGuest(int totalGuest) {
        if (this.totalGuest != totalGuest) {
            this.totalGuest = totalGuest;
            updateRedisHashField(getKey(), "totalGuest", this.totalGuest);
        }
    }

    public void addTotalGuest(int value) {
        this.totalGuest += value;
        updateRedisHashField(getKey(), "totalGuest", this.totalGuest);
    }

    public int getReceptionNum() {
        return receptionNum;
    }

    public void putReceptionNum(int receptionNum) {
        if (this.receptionNum != receptionNum) {
            this.receptionNum = receptionNum;
            updateRedisHashField(getKey(), "receptionNum", this.receptionNum);
        }
    }

    public void addReceptionNum(int value) {
        this.receptionNum += value;
        updateRedisHashField(getKey(), "receptionNum", this.receptionNum);
    }

    public long getReceptionTime() {
        return receptionTime;
    }

    public void putReceptionTime(long receptionTime) {
        if (this.receptionTime != receptionTime) {
            this.receptionTime = receptionTime;
            updateRedisHashField(getKey(), "receptionTime", this.receptionTime);
        }
    }

    public void addReceptionTime(long value) {
        this.receptionTime += value;
        updateRedisHashField(getKey(), "receptionTime", this.receptionTime);
    }

    public JsonArray getTaskRewardArray() {
        if (this.taskReward != null && this.taskReward.length() > 0) {
            return new JsonArray(this.taskReward);
        } else {
            return new JsonArray();
        }
    }

    public String getTaskReward() {
        return taskReward;
    }

    public void putTaskReward(String taskReward) {
        if (!this.taskReward.equals(taskReward)) {
            this.taskReward = taskReward;
            updateRedisHashField(getKey(), "taskReward", this.taskReward);
        }
    }

}